package com.viu.technology.dao.user;

import com.viu.technology.config.datasource.DatabaseContextHolder;
import com.viu.technology.config.datasource.DatabaseType;
import com.viu.technology.mapper.UserMapper;
import com.viu.technology.po.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao{

    private static Logger log = LoggerFactory.getLogger(UserDaoImpl.class);

    @Autowired
    private UserMapper userMapper;

    public User selectByPrimaryKey(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    public User insertUser(User user) {
        if (userMapper.insert(user) > 0) {
            return user;
        }
        return null;
    }

    public User selUserAndRoleById(String id){
        log.info("开始查询用户数据--id:" + id);
        User user = userMapper.selUserAndRoleById(id);
        log.info("user--"+user);
        return user;
    }

    @Override
    public User selUserAndRoleByPhone(String phone) {
        log.info("开始查询用户数据--phone:" + phone);
        User user = userMapper.selUserAndRoleByPhone(phone);
        log.info("user--"+user);
        return user;
    }

    /**
     * 使用数据源2
     * @param id
     * @return
     */
    public User selectUserByIdWithDataSource2(String id) {
        DatabaseContextHolder.setDatabaseType(DatabaseType.DATASOURCE2);
        return userMapper.selectByPrimaryKey(id);
    }
}
